package com.minshenglife.zanzan.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.zanzan.dao.UserDao;
import com.minshenglife.zanzan.dao.WorkConPowerDao;
import com.minshenglife.zanzan.entity.User;
import com.minshenglife.zanzan.entity.WorkConPower;
import com.minshenglife.zanzan.service.WorkConPowerService;

/**
 * 工作日志权限service
 * @author jianghaibo
 *
 */
@Component
@Transactional
public class WorkConPowerServiceImpl implements WorkConPowerService{
	Logger log = LoggerFactory.getLogger(WorkConPowerServiceImpl.class);
	
	@Resource
	WorkConPowerDao workConPowerDao;
	@Resource
	UserDao userDao;

	@Override
	public WorkConPower findByExample(WorkConPower conPower) {
		List<WorkConPower> list = workConPowerDao.findByExample(conPower);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WorkConPower> findListByExample(WorkConPower conPower) {
		return workConPowerDao.findByExample(conPower);
	}

	@Override
	public String save(String[] empIds, String powerType) {
		String empIdTemp = "";
		if(empIds != null && empIds.length > 0){
			for (String empId : empIds) {
				if(empId != null && !empId.equals("")){
					User user = new User();
					user.setEmpId(empId);
					List<User> users = userDao.findByExample(user);
					if(users != null && users.size() > 0){
						boolean flag = true;
						WorkConPower workConPower = new WorkConPower();
						workConPower.setPowerType(powerType);
						workConPower.setEmpId(empId);
						List<WorkConPower> list = workConPowerDao.findByExample(workConPower);
						if(list != null && list.size() > 0){
							flag = false;
						}
						if(flag){
							workConPowerDao.add(workConPower);
						}
					}else{
						empIdTemp += empId+"，";
					}
				}
			}
		}
		return empIdTemp;
	}

	@Override
	public void delete(String[] ids) {
		if(ids != null && ids.length > 0){
			for (String id : ids) {
				workConPowerDao.delete(Long.valueOf(id));
			}
		}
		
	}

	@Override
	public List<User> findEmpListByPowerType(String powerType) {
		return workConPowerDao.findEmpListByPowerType(powerType);
	}

	@Override
	public String findPowerUserByType(String powerType) {
		String canBeReviewUser = "";
		WorkConPower conPower = new WorkConPower();
		conPower.setPowerType(powerType);
		List<WorkConPower> powers = workConPowerDao.findByExample(conPower);
		for (WorkConPower workConPower : powers) {
			canBeReviewUser += workConPower.getEmpId() + "|";
		}
		return canBeReviewUser;
	}

	@Override
	public String findPowerUserByType(String powerType, String viewType) {
		String canBeReviewUser = "";
		Map<String, String> map = new HashMap<String, String>();
		map.put("powerType", powerType);
		map.put("viewType", viewType);
		List<WorkConPower> powers = workConPowerDao.findByPowerTypeAndViewType(map);
		for (WorkConPower workConPower : powers) {
			canBeReviewUser += workConPower.getEmpId() + "|";
		}
		return canBeReviewUser;
	}
	
}
